export default {
    name: 'pDetail',
    data() {
        return {
            product: [],
            countdown: '',
            showButton: false,
            timer: null,
            isClick: false,
            deadline2: Date.now() + 1000 * 60 * 60 * 8,
            stop: true,
        }
    },
    methods: {
        addCarts() {
            this.axios.defaults.headers.common['Authorization'] = localStorage.getItem('token')
            this.axios.post('api/addCarts', { product: this.product })
                .then(res => {
                    this.$message({
                        type: 'success',
                        message: '加入购物车成功',
                        duration: 1000
                    });
                    // 此处会出现this指向问题，因此使用箭头函数
                    setTimeout(() => {
                        this.$router.push('/index/personal/cart')
                    }, 600);
                    // setTimeout(function () {
                    //     this.$router.push('/index/personal/cart')
                    // }, 600);
                })
                .catch(err => {
                    this.$message({
                        type: 'error',
                        duration: 1000,
                        message: '加入购物车失败'
                    });
                })
        }
    },
    created() {
        let id = this.$route.query.productId
        this.axios.defaults.headers.common['Authorization'] = localStorage.getItem('token')
        this.axios.get('api/getAllBooks').then(res => {
            this.product = res.data.data.find(data => {
                return data.id == id
            })
        })
    },
}